package com.ins1st.modules.xs.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ins1st.core.R;
import com.ins1st.modules.xs.entity.Student;
import com.ins1st.modules.xs.service.ClazzService;
import com.ins1st.modules.xs.service.StudentService;
import com.ins1st.util.QueryUtil;

@Controller
@RequestMapping("/xs/student")
public class StudentAction {

	private static final String PREFIX = "modules/xs/";

	@Autowired
	private StudentService studentService;
	@Autowired
	private ClazzService clazzService;

	/**
	 * 主页
	 */
	@RequestMapping("index")
	// @NeedAuth("xs:student:index")
	public String index() {
		return PREFIX + "student_index.html";
	}

	/**
	 * 查询集合
	 */
	@RequestMapping("list")
	@ResponseBody
	public Object list(HttpServletRequest request) throws Exception {
		IPage page = QueryUtil.getPage(request);
		studentService.findPage(page);
		
		//Student student = ControllerUtils.bindParams(request, Student.class);
		//QueryWrapper query = QueryUtil.build(request);
		//IPage page = studentService.page(new Page<>(student.getPage(), student.getLimit()), query);
		
		return R.return4Page(page);
	}

	/**
	 * 添加页
	 */
	@RequestMapping("add")
	// @NeedAuth(value = "xs:student:add")
	public String add(Model model) {
		model.addAttribute("clazzList", clazzService.list());
		return PREFIX + "student_form.html";
	}

	/**
	 * 保存
	 */
	@RequestMapping("save")
	@ResponseBody
	public Object save(Student student) {
		boolean save = studentService.saveOrUpdate(student);
		if (save) {
			return R.success("保存成功");
		}
		return R.error("保存失败");
	}

	/**
	 * 修改页
	 */
	@RequestMapping("get/{id}")
	// @NeedAuth("xs:student:edit")
	public String edit(@PathVariable Long id, Model model) {
		Student student = studentService.getById(id);
		model.addAttribute("student", student);
		model.addAttribute("clazzList", clazzService.list());
		return PREFIX + "student_form.html";
	}

	/**
	 * 删除
	 */
	@RequestMapping("del/{id}")
	@ResponseBody
	public Object del(@PathVariable Long id) {
		boolean del = studentService.removeById(id);
		if (del) {
			return R.success("删除成功");
		}
		return R.error("删除失败");
	}
	
	@ResponseBody
	@RequestMapping("t1")
	public Object t1() {
		return studentService.findAll();
	}
	
	@ResponseBody
	@RequestMapping("t2")
	public Object t2() {
		return studentService.findAll2("张三", 18);
	}
	
	@ResponseBody
	@RequestMapping("t3")
	public Object t3() {
		return studentService.findPage(new Page<>(1, 10));
	}
	
	///////// 使用XML Mapper方式 ////////////
	@ResponseBody
	@RequestMapping("t4")
	public Object t4() {
		return studentService.find4();
	}
	
	@ResponseBody
	@RequestMapping("t5")
	public Object t5() {
		return studentService.find5("张三", 18);
	}
	
	@ResponseBody
	@RequestMapping("t6")
	public Object t6() {
		return studentService.find6(new Page<>(1, 10));
	}

}
